#encoding=utf-8
'''
Created on 2018-8-9

@author: cooler
'''
# from flask_mongoengine import MongoEngine
# mdb = MongoEngine()

from datetime import datetime

from mongoengine import *
mdb = connect('suface',host = '127.0.0.1',port = 27017)

#简单的策略，一个点买，一个点卖，一个止损点
class User_info(Document):
    username = StringField(required=True,unique=True) #用户名
    password = StringField(required=True) #密码
    phone = StringField(required=True)
    email = StringField(max_length=1000)
    api_key = StringField(required=True) #
    secret_key = StringField(required=True) #
    salt = StringField(required=True) #加密字符串
    role = IntField(required=True) #权限
    status = IntField(default=1)# 1 正常使用  2 禁用 3 销户
    ctime = DateTimeField(default=datetime.now)
    utime = DateTimeField(default=datetime.now)

#gateio账户
class Gateio_info(Document):
    name = StringField(required=True,unique=True)
    phone = StringField(required=True)
    email = StringField(max_length=1000)
    api_key = StringField(max_length=1000)
    secret_key = StringField(max_length=1000)
    id_rsa = StringField(max_length=1000)
    id_rsa_pub = StringField(max_length=1000)
    status = IntField(default=0)
    ctime = DateTimeField(default=datetime.now)
    utime = DateTimeField(default=datetime.now)
    user_info = ReferenceField(User_info)

    
#简单的策略，一个点买，一个点卖，一个止损点
class Strategy_info(Document):
    name = StringField(required=True) #策略名称
    currency_pair = StringField(required=True) #币种对名称
    buy_point = FloatField(required=True) #买点
    sell_point = FloatField(required=True) #卖点
    filledrate = FloatField(required=True) #成交数量
    buyrate = FloatField(default=0) #买币数量
    sellrate = FloatField(default=0) #卖币数量
    stop_point = FloatField(required=True) #止损点
    fee_total = FloatField(required=True) #盈利率
    is_loop = IntField(required=True,default=1) #是否循环执行 0 否 1 是
    status = IntField(default=0)# 0 处世状态 1 当前策略（仅能有一个） 2 停止策略
    ctime = DateTimeField(default=datetime.now)
    utime = DateTimeField(default=datetime.now)
    user_info = ReferenceField(User_info)
    gateio_info = ReferenceField(Gateio_info)

#账户钱包
class Wallet(Document):
    name_cn = StringField(required=True) # 账户名称
    amount = FloatField(required=True) #账户个数
    status = IntField(default=1)
    ctime = DateTimeField(default=datetime.now)
    utime = DateTimeField(default=datetime.now)
    user_info = ReferenceField(User_info)
    gateio_info = ReferenceField(Gateio_info)

#交易信息
class Trade_info(Document):
    order_number = IntField(required=True) # 订单编号
    in_cash = StringField(required=True)
    out_cash = StringField(required=True)
    rate = FloatField(required=True) #下单价格
    filledrate = FloatField(required=True) #成交价格
    filledAmount = FloatField(required=True) #成交数量
    leftAmount = FloatField(required=True) #剩余数量
    trade_type = IntField(default=0) #0 买入 1 卖出
    fee_total = FloatField(required=True) #交易手续费
    status = IntField(default=1)
    ctime = DateTimeField(default=datetime.now)
    utime = DateTimeField(default=datetime.now)
    user_info = ReferenceField(User_info)
    gateio_info = ReferenceField(Gateio_info)
    strategy_info = ReferenceField(Strategy_info)




class Pairs(Document):
    name = StringField(max_length=120,required=True,unique=True)
    status = IntField(default=1)
    ctime = DateTimeField(default=datetime.now)
    utime = DateTimeField(default=datetime.now)


class Market_info(Document):
    currency_pair = StringField(unique=True)
    decimal_places = IntField(required=True)
    min_amount = FloatField(required=True)
    fee = FloatField(required=True)
    status = IntField(default=1)
    ctime = DateTimeField(default=datetime.now)
    utime = DateTimeField(default=datetime.now)


#货币价格历史
class Market_trade_history(Document):
    no = IntField(required=True)
    symbol = StringField(required=True) #币种标识
    name= StringField(required=True) #币种名称
    name_en= StringField(required=True) #英文名称
    name_cn= StringField(required=True) #中文名称
    pair= StringField(required=True) #交易对
    rate= FloatField(required=True) #当前价格
    vol_a= FloatField(required=True) #被兑换货币交易量
    vol_b= FloatField(required=True) #兑换货币交易量
    curr_a= StringField(required=True) #被兑换货币
    curr_b= StringField(required=True) #兑换货币
    curr_suffix= StringField(required=True) #货币类型后缀
    rate_percent= FloatField(required=True) #涨跌百分百
    trend= StringField(required=True) #24小时趋势 up涨 down跌
    supply= FloatField(required=True) #币种供应量
    marketcap= FloatField(required=True) #总市值
    plot= StringField(required=True) #趋势数据
    status = IntField(default=1)
    ctime = DateTimeField(default=datetime.now)
    utime = DateTimeField(default=datetime.now)

class Tickets_info_history(Document):
    name_en = StringField(required=True) #币种名称
    baseVolume = FloatField(required=True) #交易量
    high24hr = FloatField(required=True) #24小时最高价
    highestBid = FloatField(required=True) #买方最高价
    last = FloatField(required=True) #最新成交价
    low24hr = FloatField(required=True) #24小时最低价
    lowestAsk = FloatField(required=True) #卖方最低价
    percentChange = FloatField(required=True) #涨跌百分比
    quoteVolume = FloatField(required=True) # 兑换货币交易量
    status = IntField(default=1)
    ctime = DateTimeField(default=datetime.now)
    utime = DateTimeField(default=datetime.now)



if __name__ == '__main__':
    # currency_pair = "eos_usdt"
    # buy_point = 4.7
    # sell_point = 4.95
    # stop_point = 4.0
    # filledrate = 1000
    # fee_total = (sell_point*(1-0.002)-buy_point*(1+0.002))

    # user = GateIo_info.objects(name='cat').first()
    # strategy = Strategy_info()
    # strategy.currency_pair=currency_pair
    # strategy.user=user
    # strategy.buy_point=buy_point
    # strategy.sell_point=sell_point
    # strategy.filledrate=filledrate
    # strategy.stop_point=stop_point
    # strategy.status=1
    # strategy.fee_total=fee_total
    # # strategy.save()
    # print fee_total
    # 
    user = User.objects.all()
    for u in user:
        print u['username']
    pass



